﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace LibMQTT.TCPServices
{
    using ProtocalIMPL;

    /// 考虑到 Cilent端 和 Server端 在初始化之后调用的方法族 从bind()函数开始不同 
    /// 这里做成基类 基类生命周期中 最后调用的函数为Bind() 
    /// TCPLegacyBase的子类 不再需要重复初始化Socket()
    /// 
    /// Client Connect() ---> 
    /// Server(TCP) Bind() ---> Listen() ---> Accept()
    /// Server(UDP) Bind() ---> 
    ///     Loop(true): 
    ///         Send[To] ---> Receive[From]
    public class MqLotBase
    {
        /* 内存堆栈区 + 静态区 */
        protected readonly int _Port;
        protected internal Socket _Listener;
        static public Encoding _BlockCoder = Encoding.UTF8;
        static public Int32 _BlockSize = short.MaxValue / 4;
        
        private MqLotBase()
        {
            /// 不管是什么类型的TCPIP协议层 始终是对伯克利Socket套接字技术的封装
            _Listener = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp
            );
        }
        public MqLotBase(int INITPort) :this()
        {
            _Port = INITPort;
        }

        /// <summary>
        /// 用户节点数据结构 不管是Client端还是Server端都应该在启动时 初始化这个数据结构
        /// </summary>
        public Lazy<LinkedList<LinkedListNode<EndPoint>>> RemotePoints =
            new Lazy<LinkedList<LinkedListNode<EndPoint>>>(() => {
                Console.WriteLine("``` [用户链表]激活 ```");
                return new LinkedList<LinkedListNode<EndPoint>>();
            }, true);

        /// <summary>
        /// MqLot程序每接受一次报文 就进行消息日志的处理工作 数据结构为队列
        /// </summary>
        public Lazy<Queue<MqPayloadVariable>> MessageTimeLine =
            new Lazy<Queue<MqPayloadVariable>>(() => {
                Console.WriteLine("``` [消息队列]激活 ```");
                return new Queue<MqPayloadVariable>();
            }, true);
    }
}
